import { obj } from "../../index.js";
import dom, { ajax, domg } from "../../init.js";
import { toarticle } from "../../navigation/eventhandle.js";

export default {
  //预览设置
  isyulan(init?: any) {
    const _obj = getArticleListObj();
    if (_obj.isyulan.checked) {
      localStorage.setItem("isyulan", "1");
      _obj.iptTitle.oninput = titleInputValue;
      _obj.iptLabel.oninput = labelInputValue;
      obj.editor.config.onchange = yulan;
      if (init === 1) return;
      titleInputValue();
      labelInputValue();
      yulan();
    } else {
      localStorage.setItem("isyulan", "");
      _obj.iptTitle.oninput = null;
      _obj.iptLabel.oninput = null;
      obj.editor.config.onchange = null;
    }
  },

  //是否跳转事件
  istoarticle(this: any) {
    if (this.checked) {
      localStorage.setItem("istoarticle", "1");
    } else {
      localStorage.setItem("istoarticle", "");
    }
  },

  //单击发布说说
  async send() {
    const title = (document.querySelector("#title") as any).value.trim();
    const content = obj.editor.txt.html().replace(/&nbsp;/g, "");
    const label = (document.querySelector("#label") as any).value.trim();
    if (!title || !content) return alert("请填写标题或内容");

    //获取时间
    const date = new Date();
    const createdTime = `${date.getFullYear()}${
      date.getMonth() + 1
    }${date.getDate()}`;
    //构建准备发送的数据

    const data = {
      title,
      titles: obj.editor.txt.text().trim().slice(0, 100) + "...",
      content,
      author: (dom.user.name as any).innerText.trim(),
      createdTime,
      browesNum: 1,
      fontlength: obj.editor.txt.text().length,
      comment: 0,
      label: label === "" ? "分享" : label,
    };
    const result = await ajax.post("/article/add", data);
    if ((result as string).includes("Error")) return alert("出错了");
    const res = JSON.parse(result as string);
    if (res.msg !== "ok") return alert("发布失败" + res.msg);
    //发布成功之后的事情
    alert("发布成功");
    if (localStorage.getItem("istoarticle")) {
      const results = await ajax.get("/article/findpk", { id: res.data.id });
      const datas = JSON.parse(results as string) as any;
      localStorage.setItem("article", JSON.stringify(datas.data));
      return (location.hash = "/MyArticle");
    }
  },
  isimgtheft() {
    if ((domg("#isimgtheft") as any).checked) {
      localStorage.setItem("isimgtheft", "1");
      obj.editor.config.uploadImgServer = "/api/user/theftarticleimg";
      console.log(obj.editor.config.uploadImgServer);
    } else {
      localStorage.setItem("isimgtheft", "");
      obj.editor.config.uploadImgServer = "/api/user/articleimg";
      console.log(obj.editor.config.uploadImgServer);
    }
  },
};

//预览试着的工具函数
function yulan() {
  (document.querySelector("#articleContent") as HTMLElement).innerHTML =
    obj.editor.txt.html().replace(/&nbsp;/g, "");
  (document.querySelector("#articleFontlength") as HTMLElement).innerText =
    obj.editor.txt.text().length;
}
function titleInputValue(this: any) {
  if ((domg("#title") as any).value === "") return;
  (document.querySelector("#articleTitle") as HTMLElement).innerText = (
    document.querySelector("#title") as any
  ).value.trim();
}
function labelInputValue(this: any) {
  if ((domg("#label") as any).value === "") return;
  (document.querySelector("#articleLabel") as HTMLElement).innerText = (
    document.querySelector("#label") as any
  ).value.trim();
}

function getArticleListObj() {
  return {
    isyulan: document.querySelector("#isyulan") as any,
    iptTitle: document.querySelector("#title") as HTMLElement,
    articleTitle: document.querySelector("#articleTitle") as HTMLElement,
    articleLabel: document.querySelector("#articleLabel") as HTMLElement,
    iptLabel: document.querySelector("#label") as HTMLElement,
    test: document.querySelector("#test") as HTMLElement,
    content: document.querySelector("#articleContent") as HTMLElement,
  };
}
